package com.aliyun.heiyu.powermaster.server.dao.impl;

import com.aliyun.heiyu.common.util.JdbcUtil;
import com.aliyun.heiyu.common.util.StringUtil;
import com.aliyun.heiyu.powermaster.server.MasterConstant;
import com.aliyun.heiyu.powermaster.server.dao.StrategyActionHistoryDao;
import com.aliyun.heiyu.powermaster.server.dao.entity.StrategyActionHistoryEntity;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class StrategyActionHistoryImpl implements StrategyActionHistoryDao {

    @Override
    public boolean insert(StrategyActionHistoryEntity entity) {
        String sql = "insert into strategy_action_history" +
            "(deviceID,actionName,time,switchOn,status,createData,dvfs,cstate,powerOff,powerLow,capping,capTo,capFmin,isFree) " +
            "values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object[] kk = new Object[14];
        kk[0] = entity.getDeviceID();
        kk[1] = entity.getActionName();
        kk[2] = entity.getTime();
        kk[3] = entity.getSwitchOn();
        kk[4] = entity.getStatus();
        kk[5] = entity.getCreateData();
        kk[6] = entity.getDvfs();
        kk[7] = entity.getCstate();
        kk[8] = entity.getPowerOff();
        kk[9] = entity.getPowerLow();
        kk[10] = entity.getCapping();
        kk[11] = entity.getCapTo();
        kk[12] = entity.getCapFmin();
        kk[13] = entity.getIsFree();
        boolean inserted = JdbcUtil.update(sql, kk);
        return inserted;
    }

    @Override
    public List<StrategyActionHistoryEntity> findOnePage(String deviceId,Integer pageSize, Integer pageNum) {
        String sql = "select * from strategy_action_history where deviceID=? order by time desc,id desc limit ?,?";
        Object[] kk = new Object[3];
        kk[0] = deviceId;
        kk[1] = (pageNum-1) * 10;
        kk[2] = pageSize;
        List<StrategyActionHistoryEntity> result = JdbcUtil.queryForList(sql,kk, StrategyActionHistoryEntity.class);
        return result;
    }

    @Override
    public int count(String deviceId) {
        String sql = "select count(*) as count from strategy_action_history where deviceID=?";
        Object[] kk = new Object[1];
        kk[0] = deviceId;
        Map<String, String> map = JdbcUtil.queryForMap(sql,kk);
        if(map == null || map.get("count") == null){
            return 0;
        }
        return Integer.valueOf(map.get("count"));
    }

    @Override
    public List<StrategyActionHistoryEntity> getByCondition(String deviceId) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("select * from strategy_action_history where 1=1");
        ArrayList<Object> list = new ArrayList<>();
        if(!StringUtil.isEmpty(deviceId)){
            buffer.append(" and deviceID=?");
            list.add(deviceId);
        }
        buffer.append(" order by time desc,id desc");
        Object[] kk = list.toArray();
        return JdbcUtil.queryForList(buffer.toString(), kk, StrategyActionHistoryEntity.class);
    }

    @Override
    public List<StrategyActionHistoryEntity> getByCondition(String deviceId, int start, int size) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("select * from strategy_action_history where 1=1");
        ArrayList<Object> list = new ArrayList<>();
        if(!StringUtil.isEmpty(deviceId)){
            buffer.append(" and deviceID=?");
            list.add(deviceId);
        }
        buffer.append(" order by time desc,id desc");
        buffer.append(" limit ?,?");
        list.add(start);
        list.add(size);
        Object[] kk = list.toArray();
        return JdbcUtil.queryForList(buffer.toString(), kk, StrategyActionHistoryEntity.class);
    }

    @Override
    public List<String> getActionStatus(String deviceId, Timestamp time) {
        List<String> actionNameList = Arrays.asList(MasterConstant.DVFS,
            MasterConstant.CSTATE, MasterConstant.POWEROFF, MasterConstant.CAPPING, MasterConstant.SHUT_DOWN);
        List<String> statusList = new ArrayList<>();
        for(String actionName : actionNameList){
            if(isActionSwitchOn(deviceId, actionName, time)){
                statusList.add(actionName);
            }
        }

        return statusList;
    }

    private boolean isActionSwitchOn(String deviceId, String actionName, Timestamp time) {
        String sql = "select * from strategy_action_history where deviceID=? and status = 1 and actionName=? and time <= ? order by time desc limit 1 offset 0";
        Object[] kk = new Object[3];
        kk[0] = deviceId;
        kk[1] = actionName;
        kk[2] = time;
        StrategyActionHistoryEntity entity  = JdbcUtil.queryForBean(sql, kk, StrategyActionHistoryEntity.class);
        if(entity == null){
            return false;
        }
        return entity.getSwitchOn();
    }
}
